import time
from db import mydb

line = 1630911600000


def counting():
    all_data = mydb['data_tmp'].find()
    # analyze = mydb['analyze']
    last = mydb['data_day']
    dict5 = {}
    dictf5 = {}
    for fg in all_data:
        item = fg['item']
        symbol = fg['symbol']
        lol = last.find_one({'symbol': symbol})
        if lol and lol['item'][-2][7] == 10 or lol['item'][-2][7] < -5:
            continue
        five_time_list = []
        five_time_dict = {}
        fifteen_time_list = []
        fifteen_time_dict = {}

        end_dict = {}
        high_dict = {'2': {}, '3': {}, '4': {}, '5': {}}
        high_dict15 = {'2': {}, '3': {}, '4': {}, '5': {}}

        r = len(item)
        flag15, tmp15 = 0, 0
        high15 = 0
        ed = 0
        for i in range(r):
            d = item[i]
            if d[0] <= line:
                continue

            tt = time.localtime(int(d[0] / 1000))
            itt = time.strftime('%Y-%m-%d', tt)
            high = d[3]
            st = d[5] - d[2]
            if tt.tm_hour == 15:
                end_dict[itt] = d[5]
            if st > 0:
                set_value_five(five_time_list, five_time_dict,
                               itt, symbol, d[9], high, high_dict)
            if tt.tm_min in [5, 20, 35, 50]:
                if ed > 0:
                    set_value_five(fifteen_time_list, fifteen_time_dict,
                                   itt, symbol, tmp15, high15, high_dict15)
                flag15 = 3
                tmp15 = 0
                high15 = 0
                ed = 0

            if flag15:
                ed += st
                if d[3] > high15:
                    high15 = d[3]
                flag15 -= 1
                tmp15 += d[9]

        for k, v in high_dict.items():
            if v:
                for z, x in v.items():
                    idx = five_time_list[five_time_list.index(z) - 1]
                    ppp = round((end_dict[z] - x) / end_dict[idx] * 100, 0)
                    if str(ppp) not in dict5:
                        dict5[str(ppp)] = 0
                    if str(ppp) + '_' + k not in dict5:
                        dict5[str(ppp) + '_' + k] = 0
                    dict5[str(ppp)] += 1
                    dict5[str(ppp) + '_' + k] += 1

        for k, v in high_dict15.items():
            if v:
                for z, x in v.items():
                    if z == five_time_list[1]:
                        continue
                    idx = fifteen_time_list[fifteen_time_list.index(z) - 1]
                    ppp = round((end_dict[z] - x) / end_dict[idx] * 100, 0)
                    if str(ppp) not in dictf5:
                        dictf5[str(ppp)] = 0
                    if str(ppp) + '_' + k not in dictf5:
                        dictf5[str(ppp) + '_' + k] = 0
                    dictf5[str(ppp)] += 1
                    dictf5[str(ppp) + '_' + k] += 1
    return dict5, dictf5


def set_value_five(time_list, time_dict, itt, symbol, cal, high, high_dict):
    if time_list:
        if itt not in time_list:
            if itt > time_list[-1]:
                oo = time_dict[time_list[-1]]
                time_dict[itt] = cal
                time_list.append(itt)
                if oo * 2 <= cal < oo * 3:
                    high_dict['2'][itt] = high
                elif oo * 3 <= cal < oo * 4:
                    high_dict['3'][itt] = high
                elif oo * 4 <= cal < oo * 5:
                    high_dict['4'][itt] = high
                elif oo * 5 <= cal:
                    high_dict['5'][itt] = high
        else:
            if time_dict[itt] < cal:
                time_dict[itt] = cal
                if len(time_list) < 2:
                    return
                oo = time_dict[time_list[-2]]
                if oo * 2 <= cal < oo * 3:
                    high_dict['2'][itt] = high
                elif oo * 3 <= cal < oo * 4:
                    high_dict['3'][itt] = high
                elif oo * 4 <= cal < oo * 5:
                    high_dict['4'][itt] = high
                elif oo * 5 <= cal:
                    high_dict['5'][itt] = high

    else:
        time_dict[itt] = cal
        time_list.append(itt)


def ccccc():
    dict5, dictf5 = counting()
    # for k, v in dict5:
    #     pass
    print(dict5)
    print(dictf5)
